<!-- 核验 -->
<template>
	<view class="scrool-page">
		<view class="list-cont">
			<scroll-view  class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(child,sn) in productList" :key="sn">
					<uni-card :title="'序号:'+(sn+1)" v-if="child.attr4 == 'true' || child.areaCode == 'CH' " style='background-color: darkgrey;'>
						<text class="uni-body">物料编码：{{ child.itemCode }}</text><br> 
						<text class="uni-body">数量：{{ child.quantityReserved }}</text><br>
						<text class="uni-body">箱号：{{ child.attr10 }}</text><br>
						<text class="uni-body" v-if="child.areaCode == 'CH'">已越库</text>
					</uni-card>
					
					<uni-card :title="'序号:'+(sn+1)" v-else>
						<text class="uni-body">物料编码：{{ child.itemCode }}</text><br> 
						<text class="uni-body">数量：{{ child.quantityReserved }}</text><br>
						<text class="uni-body">箱号：{{ child.attr10 }}</text>
					</uni-card>
				</view>					
			</scroll-view>
		</view>
		
		<view class="footer">
			<button type="default" @click="submit" >完成</button>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue'
	import { getToken } from '@/utils/auth'
	import common from '../../pages/common/common.js'
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				pkOrder : '',
				productList:[],
				content:'',
				trojanCode:'',
				warehouseLocation:'',
				status:'',
				baseUrl:getApp().globalData.config.baseUrl,
				userName:this.$store.state.user.name,
				id:'',
				orgId:getApp().globalData.config.orgId,
				allflag:false,
				messageContent:'',
				boxCode:'',
				url:'',
				areaCode:'',
				tipContent:'',
				index:'',
				userflag:'',
				idList:[],
				restList:[],
				deleteList:[],
				insertList:[],
			}
		},
		
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			this.orgId = uni.getStorageSync('orgId');
			this.pkOrder = option.pkOrder;
			this.status = option.status;
			this.title = option.title;
			this.id = option.id;
			this.index = option.index;
			uni.setNavigationBarTitle({
				title:this.title
			});
			var url = '';
			if(this.title.includes('杂收')){
				url = this.baseUrl + "/system/recpt/rcvtrans";
			}else if(this.title.includes('调入')){
				url = this.baseUrl + "/system/recpt/transferIn";
			} else if(this.title.includes('产成品入库')){
				url = this.baseUrl + "/system/recpt/recptId";
			}else if(this.title.includes('产成品核查')){
				url = this.baseUrl + "/system/pdtrcline/listCheck";
			}
			this.url = url;
			this.queryData(this.pkOrder,url);
		},
		
		onShow() {
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},
		
		created: function() {
			this.orgId = uni.getStorageSync('orgId');
		},
		
		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
			     return false;
			}
			//that.unregisterScan();
			//player.stop();
			let pages = getCurrentPages();  //获取所有页面栈实例列表
			let nowPage = pages[ pages.length - 1];  //当前页页面实例
			let prevPage = pages[ pages.length - 2 ]; 
			prevPage.$vm.chooseTab(that.index);	//上一页页面实例	
			uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
			    delta: 1
			});
			return true;
		},
		
		onHide: function() {
			this.unregisterScan();
		},
		
		destroyed: function() {
			this.unregisterScan();
		},
		
		
		methods: {
			initScan(popFlag) {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
					    let codeStr = common.utf8ByteToUnicodeStr(code);
						_this.buttonScanNew(codeStr);
						scanDevice.stopScan(); // 停止扫描						
					}
				});				
			},
			
			registerScan() {
				main.registerReceiver(receiver, filter);
			},
			
			unregisterScan() {
				main.unregisterReceiver(receiver);
			},
			
			queryData(recptId,url){
				//显示加载框
				uni.showLoading({
				    title: '加载中',
					mask:true
				});
				uni.request({
					url:url,
					data:{
						id : recptId,
						orgId:this.orgId,
						userName:this.userName,
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{
						console.log(res)
						//隐藏加载框
						uni.hideLoading();
						if(res.data.code == '200' || res.data.code == 200) {
							this.productList = res.data.rows;
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
						
					},							
				})
			},
			
			buttonScanNew(codeStr){
				if(this.status =='2'){
					uni.showToast({
						title: '任务已挂起，请先恢复',
						icon: 'none',
						duration: 3000
					});
					return;
				}			
				if(this.status =='3'){
					uni.showToast({
						title: '任务已完成，不能修改',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				if(codeStr == null || codeStr == ''){
					uni.showToast({
						title: '请扫描数据',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				var result = common.scanCode(codeStr);
				if(result[0] != "C"){
					uni.showToast({
						title: '请扫描箱号',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				this.boxCode = result[1];
				var flag = '0';
				var data = '';
				for(var i=0;i<this.productList.length;i++){
					data = this.productList[i];
					if(data.attr10 == result[1]){
						if(data.attr4 !='true' && data.areaCode != 'CH'){
							flag = '1';
						}else{
							flag = '2';
						}
						break;
					}else{
						flag = '0';
					}
				}	
				if(flag == '1'){
					data.attr4 = 'true';
					this.prompt();
				}else if(flag == '2'){
					uni.showToast({
						title: '此箱已越库或者已扫描',
						icon: 'none',
						duration: 3000
					});
					return;
				}else{
					uni.showToast({
						title: '此箱不存在',
						icon: 'none',
						duration: 3000
					});
					return;
				}
			},
						
			submit(){
				//显示加载框
				uni.showLoading({
				    title: '加载中',
					mask:true
				});
				var that = this;
				if(that.status =='2'){
					//隐藏加载框
					uni.hideLoading();
					uni.showToast({
						title: '任务已挂起，请先恢复',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				
				if(that.status =='3'){
					//隐藏加载框
					uni.hideLoading();
					uni.showToast({
						title: '任务已完成，不能修改',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				var sum = 0;
				for(var i=0;i<that.productList.length;i++){
					var data = that.productList[i];										
					if(data.attr4 != 'true' && data.areaCode != 'CH'){
						sum = sum+1;
						//var temp ={id:data.lineId};
						that.idList.push(data.lineId);
						that.deleteList.push(data.attr10);
					}else{
						that.restList.push(data.attr10);
						that.insertList.push(data.attr10)
					}				
				}
				if(sum > 0){
					uni.hideLoading();
					uni.showModal({
						title: '提示',
						content: '有'+sum+'条数据没有校验是否删除并提交?',
						success: function(res) {
							if (res.confirm) {
								that.deleteData();
							} else {											
								//隐藏加载框
								uni.hideLoading();
							}
						}
					})
					
				}else{
					that.updateData();
				}
			},
			
			deleteData(){
				//显示加载框
				uni.showLoading({
				    title: '加载中',
					mask:true
				});
				var that = this;
				uni.request({
					url:that.baseUrl+"/system/pdtrcline/deleteCheck",
					data:{
						idList : that.idList,
						orgId:that.orgId,
						userName:that.userName,
						deleteList:that.deleteList,
						insertList:that.insertList,
						recptId:that.pkOrder,
						category:'WmProductRecpt',
						startTime:'',
						endTime: '',
						id:	that.id,
						executor:that.userName,
						startTime:'',
						endTime: new Date().toISOString().slice(0, 10),
						status:'3'//完成状态
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{
						//隐藏加载框
						uni.hideLoading();
						that.idList = [];
						if(res.data.code == '200' || res.data.code == 200) {
							//that.updateData();
							uni.showToast({
								title: '核查成功并且代办任务已完成',
								icon: 'none',
								duration: 3000
							});								
							setTimeout(function(){
								let pages = getCurrentPages();  //获取所有页面栈实例列表
								let nowPage = pages[ pages.length - 1];  //当前页页面实例
								let prevPage = pages[ pages.length - 2 ]; 
								prevPage.$vm.chooseTab(that.index);
								uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});								
							},1000);
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
						
					},							
				})
			},
			
			updateData(){
				//显示加载框
				uni.showLoading({
				    title: '加载中',
					mask:true
				});
				var that = this;
				uni.request({
					url:that.baseUrl+"/system/pdtrcline/updataCheck",
					data:{
						recptId : that.pkOrder,
						orgId:that.orgId,
						userName:that.userName,
						category:'WmProductRecpt',
						startTime:'',
						endTime: '',
						id:	that.id,
						executor:that.userName,
						startTime:'',
						endTime: new Date().toISOString().slice(0, 10),
						status:'3'//完成状态
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{
						//隐藏加载框
						uni.hideLoading();
						if(res.data.code == '200' || res.data.code == 200) {
							//that.insertTask();
							uni.showToast({
								title: '核查成功并且代办任务已完成',
								icon: 'none',
								duration: 3000
							});								
							setTimeout(function(){
								let pages = getCurrentPages();  //获取所有页面栈实例列表
								let nowPage = pages[ pages.length - 1];  //当前页页面实例
								let prevPage = pages[ pages.length - 2 ]; 
								prevPage.$vm.chooseTab(that.index);
								uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});								
							},1000);
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
						
					},							
				})
			},
			
			insertTask(){
				//显示加载框
				uni.showLoading({
				    title: '加载中',
					mask:true
				});
				var that = this;
				uni.request({
					url:that.baseUrl+'/system/wmsbacklog/cx/rem/add',
					data:{
						orgId:that.orgId,
						userName:that.userName,
						pkOrder:that.pkOrder,
						category:'WmProductRecpt',
						startTime:'',
						endTime: '',
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{
						//隐藏加载框
						uni.hideLoading();
						//that.unregisterScan();
						//player.stop();
						if(res.data.code == '200' || res.data.code == 200) {	
								that.updateTask();					
								setTimeout(function(){
									let pages = getCurrentPages();  //获取所有页面栈实例列表
									let nowPage = pages[ pages.length - 1];  //当前页页面实例
									let prevPage = pages[ pages.length - 2 ]; 
									prevPage.$vm.chooseTab(that.index);
									uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
										delta: 1
									});
								},1000);							
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
						
					},			
				})
			},
						
			//更新代办任务状态为完成
			updateTask(){
				var that = this;
				//显示加载框
				uni.showLoading({
				    title: '加载中',
					mask:true
				});
				uni.request({
					url:that.baseUrl+'/system/wmsbacklog/update',
					data:{
						id:	that.id,
						executor:that.userName,
						orgId:that.orgId,
						startTime:'',
						endTime: new Date().toISOString().slice(0, 10),
						status:'3'//完成状态
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{
						//隐藏加载框
						uni.hideLoading();
						//that.unregisterScan();
						//player.stop();
						if(res.data.code == '200' || res.data.code == 200) {	
							uni.showToast({
								title: '核查成功并且代办任务已完成',
								icon: 'none',
								duration: 3000
							});								
							setTimeout(function(){
								let pages = getCurrentPages();  //获取所有页面栈实例列表
								let nowPage = pages[ pages.length - 1];  //当前页页面实例
								let prevPage = pages[ pages.length - 2 ]; 
								prevPage.$vm.chooseTab(that.index);
								uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});								
							},1000);							
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
						
					},			
				})
			},						
			
			prompt(type){
				var innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = '/static/mp3/success.mp3';
				//innerAudioContext.play();//铃声文件的路径
				//innerAudioContext.stop();
				innerAudioContext.onPlay(() => {
				  console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
				  console.log(res.errMsg);
				  console.log(res.errCode);
				});
				innerAudioContext.onPause(function() {  
					console.log('end');  
					innerAudioContext.destroy()  
				})  					
			},
		
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}
	.text-content {
		font-size: 48rpx;
		text-align: center;
	}
	
	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}
	
	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height:30px;
		font-size: 26rpx;
	}
	page {
		width: 100%;
		height: 100%;
	}
	
	
	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}
	
	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}
	
	.list-cont {
		flex: 1;
		position: relative;
	
		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}
	
		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;
	
		}
	}
	
	
	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>